<?php
namespace app\Index\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Index extends Controller
{
    public function getinfo ()
    {
        $notice = Db::name('to_notice')->order('id desc')->field(['desc','status', 'id'])->select();

        $hezuo = Db::name('to_hezuo')->order('id desc')->field(['title', 'img','id'])->select();
        $data = Db::name('to_links')->order('id desc')->field(['desc','title', 'url','id'])->select();

        $a = json([$notice, $hezuo,$data]);
        return $a;
        
    }

    public function index()

    {   
        // var_dump(Session::get('newslist'));
        if(Session::get('newslist'))
        {
            // echo 111;
            $newslist = Session::get('newslist');
        }else{
            $newslist = '';
        }
        
        $this->assign('phone',Session::get('user.phone') );


        $list = Db::name('tb_musics')->order('createtime asc')->field(['name', 'img','id'])->limit(0,8)->select();

        $result = Db::name('tb_specials')->order('addtime asc')->field(['name','imgs','id'])->limit(0,4)->select();
        $newmusic = Db::name('tb_musics')->order('createtime asc')->limit(0,5)->select();

        $singers = Db::name('tb_singers')->order('id desc')->field(['name','icon','address'])->limit(0,4)->select();


        $banner = Db::name('tb_banner')->where('status',1)->select();
        $small = Db::name('to_small')->field(['desc','img', 'id','url'])->select();
        return $this->fetch('index/index',[
                        'list'=>$list,
                        'a'=>$this->ng(),
                        'result'=>$result,
                        'newmusic'=>$newmusic,
                        'banner' =>$banner,
                        'singer' =>$singers,
                        'newslist'=>$newslist,
                        'small' => $small
                        
            ]);
       }

       
    //  机器人接口
    public function jiqir()
    {    //接收post数据
        $list = Request::instance()->post();

        if(empty($list['wt'])){
            $wt = '你叫什么';
        }else{
            $wt = $list['wt'];
        }
        
        // var_dump($list['wt']);die;

        $host = "http://jisuznwd.market.alicloudapi.com";
        $path = "/iqa/query";
        $method = "GET";
        $appcode = "63d9eed667894d1dbef06bc078ed2609";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "question=".$wt;
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        // curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }

        $data = curl_exec($curl);

        // 关闭curl
        curl_close($curl);


        // var_dump($data);
        // 处理json数据
        $jsonObj = json_decode($data);
        // var_dump($jsonObj->result->content);die;
        // 提取文章列表
    
        
         $newslist = $jsonObj->result->content;
         // var_dump($newslist);die;
         Session::set('newslist',$newslist);

          // $this->assign('newlist',$newslist);
          // return $this->fetch('index/index');
        $this->redirect('index/index');
    }



    
    
    //脑筋急转弯接口
    public function ng()
    {

        header("Content-Type: text/html; charset=utf-8");
        $num = 5; //返回数量
        $url = 'http://api.tianapi.com/txapi/naowan/?key=5f534109672e8163abf1000d429a6d09&num='.$num.'&rand';

        //API接口
        $ch = curl_init();
        $timeout = 5;
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        // var_dump(json_decode($file_contents ));
        $json = json_decode($file_contents,true);
        $list = $json['newslist'];
        return $list;
    }



    public function login()
    {
        return $this->fetch('login/login');

    }

    public function song_details($id)
    {   $this->assign('phone',Session::get('user.phone') );
       

        $list = Db::name('tb_musics')->where('id',$id)->select();

        $arr = $list[0];
        // var_dump($arr);
        $singer = Db::name('tb_singers')->where('id',$arr['singerid'])->value('name');
        // var_dump($singer);
        $special = Db::name('tb_specials')->where('id',$arr['specialid'])->value('name');

        
      

        $res = Db::table('tb_users')
        ->alias('a')
        ->join('to_comment w','a.id = w.uid')
        ->join('tb_musics m','m.id = w.mid')

        ->field('a.name,a.icon,a.phone,w.*')
        ->where('m.id',$id)
        ->select();
        // var_dump($res);die;
        $this->assign('res',$res);
        

        $re = Db::table('to_comment')
        ->alias('c')
        ->join('to_recomment r','c.id = r.cid')
        ->join('tb_users u','c.uid = u.id')
       
        ->select();

         $this->assign('re',$re);


    
 
        
           return $this->fetch('song_details/song_details',[


                        'list'=>$arr,
                        'singer'=>$singer,
                        'special'=>$special,

        ]);

    
    }


    public function singer()
    {

        //return var_dump(Session::get('notinfo'));
        return $this->fetch('singer/singer');
    }
    public function song_sheet()
    {   
        $this->assign('phone',Session::get('user.phone') );
        $list = Db::name('tb_musics')->order('createtime desc')
                          ->alias('m')
                          ->join('tb_singers s','s.id = m.singerid')
                          ->field('m.name as mname,s.name as sname,musicaddress,Lyrics,m.id as mid,m.img as mimg')
                          ->paginate(4);
        $res = Db::name('tb_music_classify')->where('parent_id',0)->select();

        foreach ($res as $k => $v) {
            $data = Db::name('tb_music_classify')->where('parent_id',$v['id'])->select();
            foreach ($data as $a => $b) {
                $result[$v['name']][] = $b['name'];
            }
        }
        // var_dump($list);2
        // var_dump($result);
        return $this->fetch('song_sheet/song_sheet',[
                'list' => $list,
                'res'  => $result,
                'name'  => '全部'
            ]);

    }

    public function singerstyle($name)
    {
         $id = Db::name('tb_music_classify')->where('name',$name)->value('id');
         // var_dump($id);
         $list = Db::name('tb_musics')->order('createtime desc')
                          ->alias('m')
                          ->join('tb_singers s','s.id = m.singerid')
                          ->where('typeid',$id)
                          ->field('m.name as mname,s.name as sname,musicaddress,Lyrics,m.id as mid,m.img as mimg')
                          ->paginate(4);

        // var_dump($list);
        foreach ($list as $k => $v) {
        
           $c = $v['mname'];
        }
        // var_dump($c);
        if (empty($c)) {
            $list = Db::name('tb_musics')->order('createtime desc')
                          ->alias('m')
                          ->join('tb_singers s','s.id = m.singerid')
                          ->where('feelid',$id)
                          ->field('m.name as mname,s.name as sname,musicaddress,Lyrics,m.id as mid,m.img as mimg')
                          ->paginate(4);
           foreach ($list as $k => $v) {
           
              $c = $v['mname'];
           } 
        }

        if(empty($c)){
            $list = Db::name('tb_musics')->order('createtime desc')
                         ->alias('m')
                         ->join('tb_singers s','s.id = m.singerid')
                         ->where('languageid',$id)
                         ->field('m.name as mname,s.name as sname,musicaddress,Lyrics,m.id as mid,m.img as mimg')
                         ->paginate(4);
        }

        $res = Db::name('tb_music_classify')->where('parent_id',0)->select();

        foreach ($res as $k => $v) {
            $data = Db::name('tb_music_classify')->where('parent_id',$v['id'])->select();
            foreach ($data as $a => $b) {
                $result[$v['name']][] = $b['name'];
            }
        }
        return $this->fetch('song_sheet/song_sheet',[
                'list' => $list,
                'res'  => $result,
                'name'  => $name
            ]);


    }


    // 新碟上架
    public function grounding()
    {
        return $this->fetch('grounding/grounding');
    }

  

    //会员中心
    public function vip_center()
    {
        return $this->fetch('vip_center/vip_center');
    }

    //关于我们
    public function about()
    {  //  关于我们
        $about = Db::name('to_about')->field(['desc','title', 'id'])->select();
        return $this->fetch('about/about',['about'=>$about]);
    }



}



